/*jslint sloppy: true */
/*global probe, angular, console */
probe.controller('CaseEditor', function ($scope, ocDialog, ocAPI, $filter, $timeout) {
	var DI = $scope.DI = $scope.$parent.DI,
		pS = $scope.$parent,
		blankCase = "//simple case\n#set ORIGIN about:blank;\n#set MAX_LOOP 100;\n" +
			"#set NEXT_LOOP_DELAY 3000;\n\nprocess main {\n}";

	pS.getCaseOptions = function (callback) {
		callback($scope['case'], $scope.dictionary);
	};
	pS.emphasizeInstruction = function (currentIns) {
		if ($scope.isCaseSimple) {
			$scope.simpleEmphasize(currentIns);
		} else {
			$scope.codeEmphasize(currentIns);
		}
	};

	// from child controller
	$scope.transCase = function (callback) {
		callback($scope['case']);
	};
	$scope.simpleEmphasize = angular.noop;
	$scope.codeEmphasize = angular.noop;

	$scope['case'] = {};
	$scope.dictionary = null;
	$scope.isCaseChanged = false;
	$scope.editorMode = 'simple';
	$scope.isCaseSimple = true;

	function getDictionaryOptions(id) {
		ocAPI.use('retrieveDictionary').by(id).then(function (data) {
			$scope.isCaseChanged = true;
			$scope.dictionary = $filter('getDictionary')(data.dictionary);
			$scope['case'].dictionaryId = id;
		});
	}
	function isCaseSimple() {
		return !!$scope['case'].code.match('//simple case');
	}
	function initializeView($case) {
		var ics = $scope.isCaseSimple = isCaseSimple(),
			em = $scope.editorMode;

		$scope.editorMode = ics ? em : 'code';
	}
	function listCase(actionName, actionFnCallback) {
		ocAPI.use("listCase").by().then(function (data) {
			ocDialog.file({
				action: actionName,
				title: '仿真测试用例',
				share: function () {
					return data.cases;
				},
				mapUpdatetime: 'updatetime',
				actionFn: function (cases, index, fileName) {
					actionFnCallback(cases[index], this, fileName);
				}
			}, DI);
		}, function (err) {
			ocDialog.alert({
				title: '获取失败',
				message: '获取测试用例列表时出错，原因：\n' + err
			}, DI);
		});
	}
	function newCase() {
		var c = $scope['case'] = {
			caseId: undefined,
			name: undefined,
			code: blankCase,
			comment: "",
			dictionaryId: 0,
			createtime: undefined,
			updatetime: undefined
		};
		$scope.isCaseSimple = true;
		$scope.dictionary = null;
		initializeView(c);
	}

	// operations on view.
	pS.newCase = $scope.newCase = $scope.$parent.newCase = function () {
		if ($scope.isCaseChanged) {
			$scope.isCaseChanged = false;
			ocDialog.alert({
				title: '未保存新建',
				message: '继续新建将丢失尚未保存的修改\n是否继续？',
				callFn: newCase
			}, DI);
		} else {
			newCase();
		}
	};
	pS.deleteCase = $scope.deleteCase = {
		title: '删除',
		message: '您当前的用例会被删除',
		callFn: function () {
			ocAPI.use('deleteCase').by($scope['case'].caseId).then(function () {
				ocAPI.cache.remove('CASE_LIST');
			}, function (err) {
				console.error(err);
			});
		}
	};
	pS.openCase = $scope.openCase = function () {
		listCase('打开', function ($case, fileDI) {
			if (!$case) {
				return;
			}
			ocAPI.use("retrieveCase").by($case.caseId).then(function (data) {
				var c;

				newCase();
				c = $scope['case'] = angular.copy(data['case']);
				$scope.isCaseChanged = false;
				fileDI.closeDialog();
				initializeView(c);
				
				if (!!c.dictionaryId) {
					getDictionaryOptions(c.dictionaryId);
				}
			});
		});
	};
	pS.saveCase = $scope.saveCase = function () {
		var c = $scope['case'];

		if ($scope['case'].caseId) {
			// The case has been existed.
			ocAPI.use("updateCase").by(c.caseId, {
				code: c.code,
				comment: c.comment,
				dictionaryId: c.dictionaryId
			}).then(function () {
				$scope.isCaseChanged = false;
			});
		} else {
			// To create a new case.
			$scope.saveAsCase('保存');
		}
	};
	pS.saveAsCase = $scope.saveAsCase = function (alias) {
		var c = $scope['case'];

		listCase(alias || '另存为', function ($case, fileDI, fileName) {
			ocAPI.use("createCase").by({
				name: fileName,
				comment: c.comment,
				code: c.code,
				dictionaryId: c.dictionaryId
			}).then(function () {
				fileDI.closeDialog();
				$scope.isCaseChanged = false;
				ocAPI.cache.remove('CASE_LIST');
			});
		});
	};
	$scope.openDictionary = function () {
		ocAPI.use("listDictionary").by().then(function (data) {
			ocDialog.file({
				action: '打开',
				title: '数据字典',
				share: function () {
					return data.dictionaries;
				},
				mapUpdatetime: 'updatetime',
				actionFn: function (dictionarys, selected) {
					var id = dictionarys[selected].dictionaryId;

					getDictionaryOptions(id);
					this.closeDialog();
				}
			}, DI);
		});
	};

	//init
	newCase();
});
